﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Identitycore.Filters
{
    /// <summary>
    /// 资源过滤器   这个过滤器在授权过滤器后执行
    /// 继承特性   实现接口iresourcefilter
    /// </summary>
    public class ResourceFilter : Attribute, IResourceFilter
    {
        /// <summary>
        /// 当Action执行完毕后进入该方法
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            
        }
        /// <summary>
        /// 当请求资源类的时候
        /// 执行Action前进入该方法 通过过滤器登陆 直接返回
        /// </summary>
        /// <param name="context"></param>
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            //FindFirstValue是扩展方法直接点不出来  视频中这个方法实现不了 获取不到值
           //string token=  context.HttpContext.User.FindFirstValue(ClaimTypes.Sid);
            //context.Result = new ContentResult() { Content = "结束执行" };
        }
    }
    /// <summary>
    /// actionr 过滤器  在 资源过滤器后执行 RESOURCE filter
    /// </summary>
    public class ActionFilter : Attribute, IActionFilter
    {
        /// <summary>
        /// 执行后进入
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }
        /// <summary>
        /// 进入前执行
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            //context.Result = new ContentResult() { Content = "结束执行" };
        }
    }
    /// <summary>
    /// 异常过滤器  在Action过滤器后执行
    /// </summary>
    public class ExceptionFilter : ExceptionFilterAttribute {
        /// <summary>
        /// 重写方法
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            context.Result = new ContentResult() { Content = "系统发生错误！" };
        }
    }

}
